package com.chixing.utils;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

//生成随机数验证码
public class RandomUtil {

    private static final Random random = new Random();
    private static final DecimalFormat fourdf = new DecimalFormat("000000");
    public static String getFourBitRandom(){
        return fourdf.format(random.nextInt(1000000));
    }

    public static ArrayList getRandom(List list, int n){
        Random random = new Random();
        HashMap<Object,Object> hashMap = new HashMap<>();

        //生成随机数并存入HashMap
        for(int i = 0; i < list.size(); i++){
            int number = random.nextInt(100) + 1;
            hashMap.put(number,i);
        }

        //从HashMap导入数组
        Object[] objects = hashMap.values().toArray();
        ArrayList r = new ArrayList();

        //遍历数组打印数据
        for(int i = 0; i < n; i++){
            r.add(list.get((int)objects[i]));
            System.out.println(list.get((int)objects[i]) + "\t");
        }
        System.out.println("\n");
        return r;
    }




}
